<?php
class Component_Appstore_Register extends appRain_Base_Component
{
    public function init()
    {
		App::Module('Hook')->setHookName('CSS')
                           ->setAction("register_css_code")
                           ->Register(get_class($this),"register_css_code");

		App::Module('Hook')->setHookName('Sitemenu')
                           ->setAction("register_sitemenu")
                           ->Register(get_class($this),"register_sitemenu");

		App::Module('Hook')->setHookName('Controller')
                           ->setAction("register_controller")
                           ->Register(get_class($this),"register_controller");


		App::Module('Hook')->setHookName('InformationSet')
                           ->setAction("register_definition")
                           ->Register(get_class($this),"register_informationset_defination");

		App::Module('Hook')->setHookName('CategorySet')
                           ->setAction("register_definition")
                           ->Register(get_class($this),"register_categoryset_defination");

		App::Module('Hook')->setHookName('InterfaceBuilder')
                           ->setAction("register_definition")
                           ->Register(get_class($this),"register_interface_builder_defination");

		App::Module('Hook')->setHookName('Sitesettings')
                           ->setAction("register_definition")
                           ->Register(get_class($this),"register_sitesettings_defination");

		App::Module('Hook')->setHookName('Addon')
                           ->setAction("register_addon")
                           ->Register(get_class($this),"register_addon_defination");

		App::Module('Hook')->setHookName('Model')
                           ->setAction("register_model")
                           ->Register(get_class($this),"register_model");

		App::Module('Hook')->setHookName('URIManager')
                           ->setAction("on_initialize")
                           ->Register(get_class($this),"register_newrole");

		App::Module('Hook')->setHookName('Helper')
                           ->setAction("register_helper")
                           ->Register(get_class($this),"register_helper");

		App::Module('Hook')->setHookName('UI')
                           ->setAction("template_left_column_A")
                           ->Register(get_class($this),"add_cart_link");

		App::Module('Hook')->setHookName('UI')
                           ->setAction("appstore_product_left_menu")
                           ->Register(get_class($this),"product_left_menu_generator");
    }

    public function init_on_install()
	{
		$this->autoRegisterAdminTopNav('catalog');
	}

    public function register_css_code()
    {
        return App::Helper('Utility')->fetchFile($this->attachMyPath('css/styles.css'));
    }

	public function register_controller()
    {
        $srcpaths = Array();
        $srcpaths[] =   array('name'=>'appStore',
                              'controller_path'=>$this->attachMyPath('controllers'));
        return $srcpaths;
    }

	public function register_informationset_defination()
    {
        $srcpaths = Array();
        $srcpaths[] =   array('type'=>'product',
                               'path'=>$this->attachMyPath('information_set/product.xml'));
		$srcpaths[] =   array('type'=>'card',
                               'path'=>$this->attachMyPath('information_set/card.xml'));
		$srcpaths[] =   array('type'=>'hoodie',
                               'path'=>$this->attachMyPath('information_set/hoodie.xml'));
		$srcpaths[] =   array('type'=>'shippingcountry',
                               'path'=>$this->attachMyPath('information_set/shippingcountry.xml'));
        return $srcpaths;
    }

	public function register_categoryset_defination()
    {
        $srcpaths = Array();
        $srcpaths[] =   array('type'=>'product-cat',
                              'path'=>$this->attachMyPath('category_set/product-cat.xml'));
        return $srcpaths;
    }

	public function register_interface_builder_defination()
    {
        $srcpaths = Array();
        $srcpaths[] = $this->attachMyPath('interface_builder/catalog.xml');
        return array('filepaths'=>$srcpaths);
    }

	public function register_model()
    {
        $srcpaths = Array();
        $srcpaths[] =   array('name'=>'Item',
                              'model_path'=>$this->attachMyPath('models'));

		$srcpaths[] =   array('name'=>'Order',
                              'model_path'=>$this->attachMyPath('models'));

		/*$srcpaths[] =   array('name'=>'Comment',
                              'model_path'=>$this->attachMyPath('models'));*/


        return $srcpaths;
    }

	public function register_sitemenu($send)
	{
		$menu = Array();
		$menu[] = Array(App::Helper('Config')->baseurl("/store"),'Store','store');
		return $menu;
	}

	public function register_newrole($def=null)
    {
        $def['pagerouter'][] = array("actual"=>Array("appstore","index"),"virtual"=>Array("store"));
		$def['pagerouter'][] = array("actual"=>Array("appstore","index","bycat"),"virtual"=>Array("store-by-cat"));
		$def['pagerouter'][] = array("actual"=>Array("appstore","product","byproduct"),"virtual"=>Array("store-by-product"));
		$def['pagerouter'][] = array("actual"=>Array("appstore","checkout"),"virtual"=>Array("checkout"));

		$def['pagerouter'][] = array("actual"=>Array("appstore","statusmessage","payment-success"),"virtual"=>Array("payment-success"));
		$def['pagerouter'][] = array("actual"=>Array("appstore","statusmessage","payment-failed"),"virtual"=>Array("payment-failed"));

        return $def;
    }

	public function register_helper()
    {
        //Default path
    }

	public function register_sitesettings_defination()
    {
        $srcpaths = Array();
        $srcpaths[] = $this->attachMyPath('sitesettings/settings.xml');
        return array('filepaths'=>$srcpaths);
    }

	public function register_addon_defination()
    {
        $srcpaths = Array();
        $srcpaths[] =   array('type'=>'appstore',
                              'path'=>$this->attachMyPath('addons/appstore.xml'));
        return $srcpaths;
    }

	public function add_cart_link($send)
	{
		$Config = App::Helper('COnfig');
		$html  = "";
		$html .= '<div class="tcartinfo" style="text-align:center;padding:10px;background-color:#F5585D;color:#FFF">';
		$html .= App::Helper('Html')->linkTag($Config->baseUrl("/checkout"),
											 $this->__('Cart') . ' (<span id="tqtycart" style="color:#FFF">'  .
											 App::Component('appStore')->Helper('Data')->getCartTotalItem() .
											 '</span>)',array('style'=>'color:#FFF'));
		$html .= '</div>';
		return $html;
	}

	public function product_left_menu_generator()
	{
		$categories = App::CategorySet('product-cat')->findAll();
		$html = '<ul class="art-vmenu">';
		foreach($categories['data'] as $category)
		{
			$html .= '<li>';
			$html .= '<a href="' . App::Helper('Config')->baseUrl("/store-by-cat/{$category['id']}/" . App::Helper('Utility')->text2normalize($category['title'])) . '"><span class="l"></span><span class="r"></span><span class="t">' . $category['title'] . '</span></a>';
			$html .= '</li>';
		}
		$html .= '</ul>';

		return $html;
	}
}